package com.sdehl.kafkatransfer.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author Jiax
 * @version 1.0.0
 * @description: DateUtil
 * @date 2023-10-12 18:23
 */
public class DateUtil {
    /**
     * 获取UTC 时间格式
     * @param date
     * @return
     */
    public static String getUTCDateStr(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormat.format(date);
    }

    /**
     * 时间格式转换
     * @param dateStr
     * @return
     */
    public static String getUTCDateStr(String dateStr){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            Date date = format.parse(dateStr);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            return dateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getUTCDateStr("2014-11-11 20:00:00 123"));
    }
}
